﻿using System;
using System.Configuration;

namespace Api.Common
{
    /// <summary>
    /// 處理配置文件中AppSetting的值
    /// </summary>
    public class ConfigHelper
    {
        public static T GetValue<T>(string key)
        {
            string cfgVal = GetConfigString(key);
            return ConvertHelper.GetValue<T, string>(cfgVal);
        }

        public static string GetConfigString(string key)
        {
            string CacheKey = "AppSettings-" + key;
            object objModel = DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = ConfigurationManager.AppSettings[key];
                    if (objModel != null)
                    {
                        DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
                        return objModel.ToString();
                    }

                    return null;
                }
                catch { }
            }
            return objModel.ToString();
        }
    }
}
